//
//  BasePickerView.m
//  ACMediaFrameExample
//
//  Created by 付顺龙 on 2019/3/11.
//  Copyright © 2019 ArthurCao. All rights reserved.
//

#import "BasePickerView.h"
#define VIEW_HEIGHT     255.0
#define TOP_HEIGHT      44.0
#define PICKER_HEIGHT   211.0
#define PICKER_ROW_HEIGHT   44.0
#define GLOBAL_LEFT_RIGHT_MARING 14.0

@implementation BasePickerView
- (instancetype)init{
    self = [super init];
    if (self) {
        [self initBaseResouce];
    }
    
    return self;
}

- (void)initBaseResouce{
    self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    //整个PickerView
    _viewBg = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenH-VIEW_HEIGHT, ScreenW, VIEW_HEIGHT)];
    _viewBg.backgroundColor = [UIColor whiteColor];
    [self addSubview:_viewBg];
    //确定、取消
    UIView* viewTop = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, TOP_HEIGHT)];
    viewTop.backgroundColor = [UIColor whiteColor];
    [_viewBg addSubview:viewTop];
    
    _btnCancel = [[UIButton alloc]initWithFrame:CGRectMake(GLOBAL_LEFT_RIGHT_MARING, 0, 60, TOP_HEIGHT)];
    [_btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [_btnCancel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [_btnCancel setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_btnCancel.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [_btnCancel addTarget:self action:@selector(onCancel) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:_btnCancel];
    
    _btnOk = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-GLOBAL_LEFT_RIGHT_MARING-60, 0, 60, TOP_HEIGHT)];
    [_btnOk setTitle:@"确定" forState:UIControlStateNormal];
    [_btnOk setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_btnOk setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_btnOk.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [_btnOk addTarget:self action:@selector(onOK) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:_btnOk];
    
}
- (void)setIsDatePicker:(BOOL)isDatePicker{
    _isDatePicker = isDatePicker;
    if (_isDatePicker) {
        if (_pickerView) {
            _pickerView.delegate = nil;
            _pickerView.dataSource = nil;
            [_pickerView removeFromSuperview];
            _pickerView = nil;
        }
        [self loadDatePickerView];
    }
    else{
        if (_datePickerView) {
            [_datePickerView removeFromSuperview];
            _datePickerView = nil;
        }
        [self loadPickerView];
    }
}
- (void)loadDatePickerView{
    if (!_datePickerView) {
        //时间picker
        _datePickerView = [[UIDatePicker alloc]init];
        _datePickerView.frame = CGRectMake(0, TOP_HEIGHT, ScreenW, PICKER_HEIGHT);
        _datePickerView.backgroundColor = [UIColor whiteColor];
        _datePickerView.minimumDate = [BasePickerView getDateFromDateString:@"1900-01-01"];
        _datePickerView.maximumDate = [NSDate date];
        _datePickerView.datePickerMode = UIDatePickerModeDate;
        [_viewBg addSubview:_datePickerView];
    }
}

- (void)loadPickerView{
    if (!_pickerView) {
        //自定义picker
        _pickerView = [[UIPickerView alloc]init];
        _pickerView.frame = CGRectMake(0, TOP_HEIGHT, ScreenW, PICKER_HEIGHT);
        _pickerView.backgroundColor = [UIColor whiteColor];
        _pickerView.showsSelectionIndicator = YES;
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        [_viewBg addSubview:_pickerView];
    }
}

- (void)show{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    CGRect frame = _viewBg.frame;
    frame.origin.y = ScreenH;
    _viewBg.frame = frame;
    _viewBg.alpha = 0;
    frame.origin.y = ScreenH - VIEW_HEIGHT;
    [window addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        _viewBg.alpha = 1;
        _viewBg.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide{
    [self removeFromSuperview];
    CGRect frame = _viewBg.frame;
    frame.origin.y = ScreenH;
    _viewBg.frame = frame;
    _viewBg.alpha = 0;
}

- (void)onCancel{
    [self hide];
}
- (void)onOK{
    [self hide];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickOK:pickerView:)]) {
        if (self.isDatePicker) {
            
            [self.delegate clickOK:_datePickerView.date pickerView:self];
        }
        else{
            NSMutableArray *arrSelect = [NSMutableArray new];
            NSInteger comCnt = [_pickerView numberOfComponents];
            for (int i = 0; i < comCnt; i++) {
                NSInteger row = [_pickerView selectedRowInComponent:i];
                NSDictionary*dic = @{KEY_COMPONENT:[NSNumber numberWithInteger:i], KEY_ROW:[NSNumber numberWithInteger:row]};
                [arrSelect addObject:dic];
            }
            id data = [self getSelectData:arrSelect];
            [self.delegate clickOK:data pickerView:self];
        }
    }
}

- (id)getSelectData:(NSArray*)arrSelect{
    return @"";
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return PICKER_ROW_HEIGHT;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 0;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return ScreenW;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
}
+ (NSDate *)getDateFromDateString:(NSString *)string {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    
    [formatter setTimeZone:zone];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    return [formatter dateFromString:string];
}
@end
